/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.editor.pipe.editor;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;

import twaver.BaseElement;
import twaver.Element;
import twaver.TDataBox;
import twaver.TWaverUtil;
import twaver.table.TElementTable;
import twaver.table.editor.ActionTableCellEditor;
import twaver.table.renderer.StringRenderer;

public class GridEditor extends ActionTableCellEditor{

	class GridEditorDialog extends JDialog {		
		public GridEditorDialog(Dialog dialog, List cellCounts){
			super(dialog);
			this.init(cellCounts);
		}
		public GridEditorDialog(Frame frame, List cellCounts){
			super(frame);
			this.init(cellCounts);
		}
		public void init(final List cellCounts){
			final TDataBox box = new TDataBox();
			final TElementTable table = new TElementTable(box);
			final JTextField countText = new JTextField();
			
			JButton rightButton = new JButton(TWaverUtil.getIcon("/resource/image/direction/e.png"));
			JButton leftButton = new JButton(TWaverUtil.getIcon("/resource/image/direction/w.png"));
			JPanel countPanel = new JPanel(new BorderLayout());
			countPanel.add(leftButton, BorderLayout.WEST);
			countPanel.add(rightButton, BorderLayout.EAST);
			countPanel.add(countText, BorderLayout.CENTER);	
			
			JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			JButton ok = new JButton(TWaverUtil.getString("CONFIRM"));
			JButton cancel = new JButton(TWaverUtil.getString("CANCEL"));
			bottomPanel.add(ok);
			bottomPanel.add(cancel);
			
			table.setTableBodyPopupMenuFactory(null);
			table.setTableHeaderPopupMenuFactory(null);
			table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
			table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			table.setElementClass(BaseElement.class);
			table.registerElementClassXML(BaseElement.class, "/demo/editor/pipe/editor/GridEditor.xml");
			table.getColumnByName("ID").setDisplayName(TWaverUtil.getString("INDEX"));
			table.getColumnByName("count").setDisplayName(TWaverUtil.getString("VALUE"));
			
			this.getContentPane().setLayout(new BorderLayout());
			this.getContentPane().add(countPanel, BorderLayout.NORTH);
			this.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);		
			this.getContentPane().add(bottomPanel, BorderLayout.SOUTH);
			
			for(int i=0; i<cellCounts.size(); i++){
				Element element = new BaseElement(TWaverUtil.valueOf(i));
				element.putClientProperty("count", cellCounts.get(i));
				box.addElement(element);
			}
			
			countText.setHorizontalAlignment(JTextField.CENTER);
			countText.setEditable(false);
			countText.setText(box.size() + "");
			
			leftButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					int count = box.size();
					if(count > 0){
						box.removeElementByID(TWaverUtil.valueOf(count-1));
						countText.setText(box.size() + "");
					}
				}
			});
			rightButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					int count = box.size();
					Element element = new BaseElement(TWaverUtil.valueOf(count));
					element.putClientProperty("count", TWaverUtil.valueOf(3));
					box.addElement(element);
					countText.setText(box.size() + "");
				}
			});
			ok.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					cellCounts.clear();
					for(int i=0; i<box.size(); i++){
						Element element = box.getElementByID(TWaverUtil.valueOf(i));
						Integer count = (Integer)element.getClientProperty("count");
						if(count.intValue() != 0){
							cellCounts.add(count);
						}
					}
					GridEditorDialog.this.dispose();
					GridEditor.this.setValue(cellCounts);
					GridEditor.this.stopCellEditing();
					table.requestFocus();
				}
			});
			cancel.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					GridEditorDialog.this.dispose();
					GridEditor.this.cancelCellEditing();
					table.requestFocus();
				}
			});
		}
		
	}
	
	public GridEditor() {
		super(new StringRenderer("center", "true"));
	}

	public void actionPerformed(ActionEvent e) {
		List cellCounts = (List)this.getValue();
		if(cellCounts != null){
			GridEditorDialog dialog = null;
	    	Window window = (Window)SwingUtilities.getAncestorOfClass(Window.class, this.getTable());
	        if (window instanceof Dialog) {
	            dialog = new GridEditorDialog( (Dialog) window, cellCounts);
	        } else if(window instanceof Frame){
	            dialog = new GridEditorDialog( (Frame) window, cellCounts);
	        }else{
	        	dialog = new GridEditorDialog( (Frame) null, cellCounts);
	        }
	        dialog.setSize(180, 200);
	        dialog.setModal(true);
			TWaverUtil.centerWindow(dialog);	
	        dialog.setVisible(true);
	        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		}
	}
	

}